home *** CD-ROM | disk | FTP | other *** search
/ Loadstar 24 / 024.d81 / t.garbage collec < prev    next >
Text File  |  2022-08-26  |  6KB  |  359 lines

  1.  
  2.  
  3.   GARBAGE COLLECTING EXPLAINED--
  4.  
  5.            ONE SOLUTION
  6.                 by
  7.             Dick Bull
  8.  
  9.  
  10.   When writing and running file
  11.  
  12. programs in BASIC, I would run into
  13.  
  14. an apparent lockup in my 64.  I had
  15.  
  16. read some articles on garbage
  17.  
  18. collection.  Could this be my
  19.  
  20. problem??  After rereading the
  21.  
  22. articles I was convinced that this
  23.  
  24. was indeed what was happening and
  25.  
  26. went along with the authors' opinion
  27.  
  28. that nothing could be done about it.
  29.  
  30. When working on my last program,
  31.  
  32. however, it became a major problem,
  33.  
  34. so I went back to the research.
  35.  
  36.  
  37.   Microsoft BASIC used by Commodore
  38.  
  39. does not set aside a specific
  40.  
  41. location to store its strings in.
  42.  
  43. Instead it utilizes any unused memory
  44.  
  45. available, starting at the highest
  46.  
  47. address used by BASIC, 40959, and
  48.  
  49. works down until it's about to
  50.  
  51. collide with the BASIC arrays. BASIC
  52.  
  53. arrays store themselves working
  54.  
  55. upwards. You can't possibly use up
  56.  
  57. the 30k remaining after your program,
  58.  
  59. can you?.. Yes!!! Here comes the
  60.  
  61. garbage.  A simple program can use up
  62.  
  63. a lot of "string memory":
  64.  
  65. 10 B$="bill": print "[clr]"
  66. 20 input "Your name";A$
  67. 30 print "Hi "A$
  68. 35 if A$=B$ then 60
  69. 40 print "sorry I am not looking for
  70. you today"
  71. 50 for t= 1 to 2000: next: goto 10
  72. 60 print "lets get to work"
  73. 70 rem rest of program
  74.  
  75.  
  76.   Each time a new name is entered it
  77.  
  78. uses up memory, not permanently like
  79.  
  80. a program or the strings you want to
  81.  
  82. keep, but temporarily until the
  83.  
  84. computer needs the room. Suppose I
  85.  
  86. were to enter "Fred" to the prompt in
  87.  
  88. line 20. The computer would store it
  89.  
  90. like this:
  91.  
  92.  
  93.      40949 = d
  94.      40958 = e
  95.      40957 = r
  96.      40956 = F
  97.  
  98.  
  99.   The computer then sets the pointer
  100.  
  101. for a$ (elsewhere in memory) to point
  102.  
  103. to 40956. "Fred" is not what the
  104.  
  105. program was looking for so the
  106.  
  107. program goes back to 10.  Next I enter
  108.  
  109. "Tom"; the computer continues to store
  110.  
  111. in a downward direction and changes
  112.  
  113. the pointer for a$ to start at 40953.
  114.  
  115.  
  116.      40955 = m
  117.      40954 = o
  118.      40953 = T
  119.  
  120.  
  121. "Fred" 40956 - 40959 becomes useless
  122.  
  123. garbage. The computer is content to
  124.  
  125. waste this space until it is needed.
  126.  
  127. When the 64 goes to store a string
  128.  
  129. and can't find enough room, it takes
  130.  
  131. out the garbage, crunching any
  132.  
  133. strings that are still needed toward
  134.  
  135. the top of memory.
  136.  
  137.  
  138.   This garbage collection routine can
  139.  
  140. take considerable time and your
  141.  
  142. computer will appear to lock up.
  143.  
  144. I don't know the maximum time it can
  145.  
  146. take, but I know I have hit the
  147.  
  148. run/stop key and finally the
  149.  
  150. [run/stop restore] out of desperation
  151.  
  152. thinking the computer had gone to
  153.  
  154. never never land. It will take about
  155.  
  156. nineteen minutes for 3800 strings to
  157.  
  158. be garbage collected.
  159.  
  160.  
  161. WHAT AFFECTS THE TIME IT TAKES?
  162.  
  163.  
  164.   There are two variables: how long
  165.  
  166. will the garbage collection take and
  167.  
  168. how often will it be needed. The
  169.  
  170. number of valid strings and the
  171.  
  172. number of string arrays declared in a
  173.  
  174. dim statement have the greatest effect
  175.  
  176. on the time needed to garbage
  177.  
  178. collect.  The amount of garbage
  179.  
  180. generated and the length of the
  181.  
  182. strings still needed does not have a
  183.  
  184. significant effect on the time
  185.  
  186. required.  They will ,however,
  187.  
  188. determine how often garbage
  189.  
  190. collection is needed.
  191.  
  192.  
  193.   Doubling either the number of
  194.  
  195. strings used or the number
  196.  
  197. dimensioned doubles the time
  198.  
  199. required. A variable dimensioned to
  200.  
  201. 200 and with 100 used will take about
  202.  
  203. 1.5 seconds; dimed to 500 and 250
  204.  
  205. used takes 8.4 seconds; 3200
  206.  
  207. dimensioned and used will take 13
  208.  
  209. minutes and 29 seconds to crunch the
  210.  
  211. strings and clear the garbage.
  212.  
  213.  
  214. IS THERE A SOLUTION?
  215.  
  216.  
  217.   You can force the computer to
  218.  
  219. garbage collect at a time convenient
  220.  
  221. to you by using the fre(0) command.
  222.  
  223. This is what I tried first, forcing a
  224.  
  225. collection when returning to the main
  226.  
  227. menu. It soon became a pain in the
  228.  
  229. neck to wait, even if the computer
  230.  
  231. had just done the job. I had read
  232.  
  233. that you could not predict when
  234.  
  235. collection was going to occur so this
  236.  
  237. was the best that I could manage.  I
  238.  
  239. could tell from the partially
  240.  
  241. finished menu screen that it was
  242.  
  243. garbage collecting and not locked up.
  244.  
  245.  
  246.   Thinking of my predicament, I kept
  247.  
  248. coming back to the fact that if the
  249.  
  250. computer knew when it needed to
  251.  
  252. collect, why couldn't I know too?
  253.  
  254. Looking at a memory map I discovered
  255.  
  256. the solution.  Memory locations 51
  257.  
  258. and 52 contained the pointer to the
  259.  
  260. bottom of string storage.  Location
  261.  
  262. 49 and 50 contained the pointer to
  263.  
  264. the top of basic arrays.  A one line
  265.  
  266. computation would tell me the amount
  267.  
  268. of unused memory available before
  269.  
  270. garbage collection was necessary:
  271.  
  272.  
  273. 300 f= peek(52)*256 + peek(51) -
  274.     peek(50)*256 - peek(49)
  275.  
  276.  
  277. Not needing an exact figure I was
  278.  
  279. able to shorten the test and write
  280.  
  281. the following routine that prints a
  282.  
  283. prompt and forces collection if it
  284.  
  285. would be needed soon:
  286.  
  287.  
  288. 300 IF PEEK(52)-PEEK(51)>1 THEN
  289.     RETURN
  290.  
  291. 310 PRINT"[home][red]TAKING OUT THE
  292. GARBAGE-BACK IN A MOMENT[cyn]"
  293.  
  294. 320 FR=FRE(0):PRINT"[csr up][39
  295. spaces]":POKE 198,0 : RETURN
  296.  
  297.  
  298.   It is then only necessary to place
  299.  
  300. a gosub 300 after each string
  301.  
  302. manipulation.  Line 300 checks for
  303.  
  304. at least 256 free bytes left.  If so,
  305.  
  306. it returns to your program.  If the 1
  307.  
  308. in >1 is increased the routine can be
  309.  
  310. called less often. The max input#
  311.  
  312. length is 80 charactors, so with >1
  313.  
  314. it could be called every three
  315.  
  316. inputs, 3 * 80 = 240.
  317.  
  318.  
  319.   If it is time to collect garbage
  320.  
  321. the prompt in 310 is printed,
  322.  
  323. collecting is accomplished, and
  324.  
  325. the prompt is erased by line 320. By
  326.  
  327. poking 198 with 0 it clears the
  328.  
  329. keyboard buffer in case someone has
  330.  
  331. been impatiently pressing keys. The
  332.  
  333. prompt gives the indication of what
  334.  
  335. is currently going on within the
  336.  
  337. apparently locked-up computer. The
  338.  
  339. colors and the positioning used can
  340.  
  341. of course be altered to fit the
  342.  
  343. program.
  344.  
  345.  
  346.   It is reassuring to see the prompt
  347.  
  348. come on and then clear before
  349.  
  350. execution is resumed. I also added
  351.  
  352. sound routines at the start and end
  353.  
  354. to alert me to the change in program
  355.  
  356. status.
  357.  
  358. -----------<end of text>-------------
  359.